package rails;

import java.util.ArrayList;


/**
 * This class represents the switches of the train network, junctions with more than three rails.
 * It has two lists of rails known as amont and aval which are rails coming to the switch.
 * The two integers sortieAmont and sortieAval allow us to know the index of the rails in the list that are chosen as exits.
 * 
 * @author BERNARD Arthur, DELAMARCHE J�r�me, LE STER Olivier, PAVAGEAU Max
 *
 */
public class JAiguillage extends Jonction {
	/**
	 * A list of rails at one side of the junction.
	 * 
	 * @see Rail
	 */
	private ArrayList<Rail> amont;
	
	/**
	 * A list of rails at the other side of the junction.
	 */
	private ArrayList<Rail> aval;
	
	/**
	 * A integer to know which rail in amont is know as an exit for the junction.
	 */
	private int sortieAmont;
	
	/**
	 * A integer to know which rail in aval is know as an exit for the junction.
	 */
	private int sortieAval;

	/**
	 * A basic constructor.
	 */
	public JAiguillage() {
		amont = null;
		aval = null;
		sortieAmont = 0;
		sortieAval = 0;
	}
	
	/**
	 * A constructor where we can give informations about the two lists of rails and which ones are selected as exits.
	 * 
	 * @param amont A list of rails in amont.
	 * @param aval A list of rails in aval.
	 * @param sortieAmont The index (in the list) of the rail in amont as exit.
	 * @param sortieAval The index (in the list) of the rail in aval as exit.
	 */
	public JAiguillage(ArrayList<Rail> amont, ArrayList<Rail> aval, int sortieAmont, int sortieAval) {
		this.amont = amont;
		this.aval = aval;
		this.sortieAmont = sortieAmont;
		this.sortieAval = sortieAval;
	}

	/**
	 * Sets the exit in amont as index.
	 * 
	 * @param index The index of the element in the amont list specified as an exit.
	 */
	public void setSortieAmont(int index) {
		this.sortieAmont = index;
	}
	
	/**
	 * Sets the exit rail in amont.
	 * 
	 * @param rail The exit rail in amont list.
	 */
	public void setSortieAmont(Rail rail) {
		int i = 0;
		
		while(amont.get(i) != null) { //On cherche l'index du rail en question.
			if(amont.get(i).equals(rail)) {
				setSortieAmont(i);
				break;
			}
			
			i++;
		}
	}
	
	/**
	 * Sets the exit in aval as index.
	 * 
	 * @param index The index of the element in the aval list specified as an exit.
	 */
	public void setSortieAval(int index) {
		this.sortieAval = index;
	}
	
	/**
	 * Sets the exit rail in aval.
	 * 
	 * @param rail The exit rail in aval list.
	 */
	public void setSortieAval(Rail rail) {
		int i = 0;
		
		while(aval.get(i) != null) { //On cherche l'index du rail en question.
			if(aval.get(i).equals(rail)) {
				setSortieAval(i);
				break;
			}
			
			i++;
		}
	}

	/**
	 * Sets the list of rails in amont of this junction.
	 * 
	 * @param amont The list of rails.
	 */
	public void setAmont(ArrayList<Rail> amont) {
		this.amont = amont;
	}
	
	/**
	 * Gets the list of rails in amont of this junction.
	 * 
	 * @return The list of rails.
	 */
	public ArrayList<Rail> getAmont() {
		return this.amont;
	}
	

	/**
	 * Sets the list of rails in aval of this junction.
	 * 
	 * @param amont The list of rails.
	 */
	public void setAval(ArrayList<Rail> aval) {
		this.aval = aval;
	}
	
	/**
	 * Gets the list of rails in aval of this junction.
	 * 
	 * @return The list of rails.
	 */
	public ArrayList<Rail> getAval() {
		return this.aval;
	}
	
	public Rail getSortieAmont() {
		return amont.get(sortieAmont);
	}
	
	public Rail getSortieAval() {
		return aval.get(sortieAval);
	}
 
	public Rail railSuivant(Rail rail) {
		if(rail.equals(amont.get(sortieAmont)))
			return aval.get(sortieAval);
		else if(rail.equals(aval.get(sortieAval)))
			return amont.get(sortieAmont);
		else {//Le train ne peut pas acc�der � l'aiguillage
			System.out.println("D�raillement du train !");
			System.exit(1);
			return null;
		}		
	}
	
	public String toString() {
		return "Je suis un aiguillage";
	}
}
